/**
 * Created by no1 on 2015/7/24.
 */
'use strict';
let DD = require('./daoLoader');
let F = require('../function')
var Base = require('./Base');
var util = require("util");
let districtDao = new DD.mongoDB.DistrictDao();
function DistrictDispatcher(data) {
  data = data || {};
  data.dao = data.dao || districtDao;
  data.esDao = data.esDao || {};
  data.redisDao = data.redisDao || {};
  data.modelName = data.modelName || 'district';
  data.modelType = data.modelType || 'mongo';
  Base.call(this, data);
};

util.inherits(DistrictDispatcher, Base);


/**
 * 获取列表
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DistrictDispatcher.prototype.info = function*(fields) {
  let ret;
  let type = fields.type;
  if (type === 'tree') ret = yield this.dao.findTreeByCode(fields.code);
  else if (type === 'directChildren') ret = yield this.dao.findByCodeWithDirectChildren(fields.code);
  else if (type === 'allChildren') ret = yield this.dao.findByCodeWithAllChildren(fields.code);
  else if (type === 'near') ret = yield this.dao.findByCodeWithAllChildren(fields.code);
  else if (type === 'single') ret = yield this.dao.findByCode(fields.code);
  else return F.returnData(1, 'type必须为 [tree,directChildren,allChildren,near,single] 中的一种');
  //let data = JSON.parse(JSON.stringify(ret));
  //data = F.dataFilter(data, [ 'loc'], true);
  return F.returnData(0, ret.msg, ret);
}


/**
 * @todo 添加
 * @param data
 * @returns {{error, msg, data}|{error, msg}}
 */
DistrictDispatcher.prototype.add = function*(fields) {

  let modelInstance = {
    name: fields.name,
    code: fields.code,
    pCode: fields.pCode,
    loc: fields.loc,
    level: fields.level
  };
  let ret = yield this.dao.save(modelInstance);
  return F.returnData(0, ret.msg, ret.data);

};

/**
 * @todo 获取详情
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DistrictDispatcher.prototype.delete = function*(fields) {
  let ret = yield this.dao.deleteByCode(fields.code);
  return F.returnData(0, ret.msg, ret.data);
}

/**
 * @todo 获取最近
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DistrictDispatcher.prototype.near = function*(fields) {
  let defaultMaxDistance = 500000;
  let defaultLimit = 20;
  let defaultPage = 1;

  let limit = fields.pageSize || defaultLimit;
  let page = fields.page || defaultPage;
  let skip = (page - 1) * limit;
  let ret = yield this.dao.findNear(fields.loc, fields.radius, limit, skip);
  return F.returnData(0, '查询成功', ret);
}

/**
 * @todo 获取城市
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DistrictDispatcher.prototype.city = function*(fields) {
  var ret = yield this.dao.findCity(fields.pCode);
  let data = JSON.parse(JSON.stringify(ret));
  data = F.dataFilter(data, ['_id', 'updateAt', 'loc'], true);
  return F.returnData(0, 'ok', data);
}

DistrictDispatcher.prototype.list = function*(fields) {
  let parameters = {}
    , opt = {}
    , arrDel = ['_id', '__v', '__t', 'createdAt', 'updateAt', 'status','ts','loc']
    , pCode = fields.pCode || 0
  for (let one of arrDel) opt[one] = 0;
  let ret = yield this.dao.findBypCode(pCode, opt);
  return F.returnData(0, ret.msg, ret.data);
}

/**
 * @todo 获取省+直辖市
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DistrictDispatcher.prototype.province = function*(fields) {
  var ret = yield this.dao.findProvice();
  let data = JSON.parse(JSON.stringify(ret));
  data = F.dataFilter(data, ['_id', 'updateAt', 'loc'], true);
  return F.returnData(0, 'ok', data);
}

DistrictDispatcher.prototype.jointDistrictByAtLastCode = function*(code) {
  let retData = [];
  let pCode = code;
  let ret;
  for (let i = 0; i <= 5; i++) {
    ret = yield this.dao.getOneByQuery({code: pCode});
    if (ret.error == 1) F.returnData(1, ret.msg, ret.data);
    ret = ret.data;
    retData.unshift({code: ret.code, name: ret.name, pCode: ret.pCode, level: ret.level});
    if (ret.pCode != 0) pCode = ret.pCode;
    else break;
  }
  return F.returnData(0, 'ok', retData);
}

/**
 * 内部调用
 * @param Que
 * @returns {{error, msg, data}|{error, msg}}
 */
DistrictDispatcher.prototype.findByDis = function*(Que) {
  let arrDel = ['code', 'name', 'type', 'status', 'pCode','level']
    , opt = {};
  for (let one of arrDel) opt[one] = 1;
  let ret = yield  this.dao.getByQuery(Que, opt);
  let json = JSON.parse(JSON.stringify({data: ret.data}));
  let retData = F.dataFilter(json, ['_id']);
  if (ret.error == 1) return F.returnData(1, ret.msg, retData.data);
  return F.returnData(0, ret.msg, retData.data);
};

module.exports = DistrictDispatcher;
